package com.cloud.www.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.www.model.entity.MCountry;
import com.cloud.www.model.entity.MFilm;
import com.cloud.www.model.entity.MLanguage;
import com.cloud.www.model.entity.MType;
import com.cloud.www.service.MCountryService;
import com.cloud.www.service.MFilmService;
import com.cloud.www.service.MLanguageService;
import com.cloud.www.service.MTypeService;
import com.cloud.www.util.BlogPageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@AllArgsConstructor
@RequestMapping("")
public class ListController extends BaseController {

    private final MFilmService mFilmService;
    private final MTypeService mTypeService;
    private final MCountryService mCountryService;
    private final MLanguageService mLanguageService;

    /**
     * 列表
     */
    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request,
                             HttpServletResponse response,
                             MFilm req
    ) {
        Page page = new Page();
        page.setCurrent(Convert.toInt(request.getParameter("current"), 1));
        page.setSize(12);
        String title = "";
        IPage<MFilm> mFilmIPage = mFilmService.getList(page, req);
        ModelAndView view = baseView(title, "list/list");
        view.addObject("listData", mFilmIPage.getRecords());
        BlogPageHelper blogPageHelper = new BlogPageHelper();
        String pageData = blogPageHelper.getBlogPageHTML(page, request);
        view.addObject("pageData", pageData);

        Integer oneTypeId = req.getOneTypeId();
//        同级分类
        if (Convert.toInt(oneTypeId, 0) <= 0 && Convert.toInt(req.getTwoTypeId(), 0) > 0) {
            MType mType = mTypeService.getById(req.getTwoTypeId());
            oneTypeId = mType.getPid();
        }
        if (oneTypeId!=null){
            List<MType> listTypes = mTypeService.selectAll(new MType().setPid(oneTypeId));
            MType mType = mTypeService.getById(oneTypeId);
            setTypeData(req, view, listTypes);
            view.addObject("topNav", "<a href=\"/list/o_" + mType.getId() + ".html\">" + mType.getTypeName() + "</a>");
        }


//分类
//        出品地区
        setCountryData(req, view);
//        出品语言
        setLanguageData(req, view);
//        出品年份
        setYearData(req, view);

        return view;
    }

    private void setYearData(MFilm req, ModelAndView view) {
        StringBuilder sbType = new StringBuilder();
        sbType.append("<div class=\"lower\">出品年份：</div>\n");
        int year = DateUtil.year(new Date()); // new Date().getYear();

//        String typeUrl = "/list?oneTypeId=" + req.getOneTypeId() + "&twoTypeId=" + req.getTwoTypeId() +
//                "&countryId=" + req.getCountryId() + "&languageId=" +
//                req.getLanguageId() + "&year={year}";
        String typeUrl = hrefUrl(req, "year");

        for (int i = year; i >= 2005; i--) {
            sbType.append("<li><a href=\"" + typeUrl.replace("{year}", i + "") + "\" title=\"" + i + "\"><i class=\"iconfont\"></i>" + i + "</a></li>");
        }
        sbType.append("<li><a href=\"" + typeUrl.replace("{year}", "more") + "\" title=\"更早\"><i class=\"iconfont\"></i>更早</a></li>");

        view.addObject("yearData", sbType.toString());
    }

    private String hrefUrl(MFilm req, String condition) {
        HashMap<String, Object> map = new HashMap<>();
        if (req.getOneTypeId() != null && !condition.equals("oneTypeId")) {
            map.put("oneTypeId", req.getOneTypeId());
        }
        if (req.getTwoTypeId() != null && !condition.equals("twoTypeId")) {
            map.put("twoTypeId", req.getTwoTypeId());
        }
        if (req.getCountryId() != null && !condition.equals("countryId")) {
            map.put("countryId", req.getCountryId());
        }
        if (req.getLanguageId() != null && !condition.equals("languageId")) {
            map.put("languageId", req.getLanguageId());
        }
        if (req.getYear() != null && !condition.equals("year")) {
            map.put("year", req.getYear());
        }
//        StringBuilder sb = new StringBuilder();
        String url = "/list?" + condition + "={" + condition + "}";
        for (String item : map.keySet()) {
            String val = Convert.toStr(map.get(item));
            url = url + "&" + item + "=" + val;
        }
        return url;
    }

    private void setLanguageData(MFilm req, ModelAndView view) {
        List<MLanguage> listLanguage = mLanguageService.selectAll(null);
        if (listLanguage.size() > 0) {
            StringBuilder sbType = new StringBuilder();
            sbType.append("<div class=\"lower\">出品语言：</div>\n");
            String typeUrl = hrefUrl(req, "languageId");

            for (MLanguage languageItem : listLanguage) {
//                String typeUrl = "/list?" + getKeyData("oneTypeId", req.getOneTypeId() + "") + "&twoTypeId=" + req.getTwoTypeId() +
//                        "&countryId=" + req.getCountryId() + "&languageId=" +
//                        languageItem.getId() + "&year=" + req.getYear();
                String replaceUrl = typeUrl.replace("{languageId}", languageItem.getId() + "");
                sbType.append("<li><a href=\"" + replaceUrl + "\" title=\"" + languageItem.getTypeName() + "\"><i class=\"iconfont\"></i>" + languageItem.getTypeName() + "</a></li>");
            }
            view.addObject("languageData", sbType.toString());
        }
    }

//    private String getKeyData(String key, String val) {
//        if (StrUtil.isEmpty(val)) {
//            return "";
//        }
//        return key + "=" + val;
//    }

    //    出品地区
    private void setCountryData(MFilm req, ModelAndView view) {
        List<MCountry> listCountry = mCountryService.selectAll(null);
        if (listCountry.size() > 0) {
            StringBuilder sbType = new StringBuilder();
            sbType.append("<div class=\"lower\">出品地区：</div>\n");
            String typeUrl = hrefUrl(req, "countryId");
            for (MCountry countryItem : listCountry) {
//                String typeUrl = "/list?oneTypeId=" + req.getOneTypeId() + "&twoTypeId=" + req.getTwoTypeId() +
//                        "&countryId=" + countryItem.getId() + "&languageId=" +
//                        req.getLanguageId() + "&year=" + req.getYear();
                sbType.append("<li><a href=\"" + typeUrl.replace("{countryId}", countryItem.getId() + "") + "\" title=\"" + countryItem.getTypeName() + "\"><i class=\"iconfont\"></i>" + countryItem.getTypeName() + "</a></li>");
            }
            view.addObject("countryData", sbType.toString());
        }
    }

    private void setTypeData(MFilm req, ModelAndView view, List<MType> listTypes) {

        if (listTypes.size() > 0) {
            StringBuilder sbType = new StringBuilder();
            sbType.append("<div class=\"lower\">同级分类：</div>\n");
            String typeUrl = hrefUrl(req, "twoTypeId");
            for (MType typeItem : listTypes) {
//                String typeUrl = "/list?oneTypeId=" + req.getOneTypeId() + "&twoTypeId=" + typeItem.getId() +
//                        "&countryId=" + req.getCountryId() + "&languageId=" +
//                        req.getLanguageId() + "&year=" + req.getYear();
                sbType.append("<li><a href=\"" + typeUrl.replace("{twoTypeId}", typeItem.getId() + "") + "\" title=\"" + typeItem.getTypeName() + "\"><i class=\"iconfont\"></i>" + typeItem.getTypeName() + "</a></li>");
            }
            view.addObject("typeData", sbType.toString());
        }
    }

}
